package cn.iocoder.yudao.module.train.controller.admin.coursegroup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.coursegroup.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursegroup.CourseGroupDO;
import cn.iocoder.yudao.module.train.service.coursegroup.CourseGroupService;

@Tag(name = "管理后台 - 课程组管理")
@RestController
@RequestMapping("/train/course-group")
@Validated
public class CourseGroupController {

    @Resource
    private CourseGroupService courseGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建课程组管理")
    @PreAuthorize("@ss.hasPermission('train:course-group:create')")
    public CommonResult<Integer> createCourseGroup(@Valid @RequestBody CourseGroupSaveReqVO createReqVO) {
        return success(courseGroupService.createCourseGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程组管理")
    @PreAuthorize("@ss.hasPermission('train:course-group:update')")
    public CommonResult<Boolean> updateCourseGroup(@Valid @RequestBody CourseGroupSaveReqVO updateReqVO) {
        courseGroupService.updateCourseGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程组管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:course-group:delete')")
    public CommonResult<Boolean> deleteCourseGroup(@RequestParam("id") Integer id) {
        courseGroupService.deleteCourseGroup(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程组管理")
                @PreAuthorize("@ss.hasPermission('train:course-group:delete')")
    public CommonResult<Boolean> deleteCourseGroupList(@RequestParam("ids") List<Integer> ids) {
        courseGroupService.deleteCourseGroupListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程组管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:course-group:query')")
    public CommonResult<CourseGroupRespVO> getCourseGroup(@RequestParam("id") Integer id) {
        CourseGroupDO courseGroup = courseGroupService.getCourseGroup(id);
        return success(BeanUtils.toBean(courseGroup, CourseGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程组管理分页")
    @PreAuthorize("@ss.hasPermission('train:course-group:query')")
    public CommonResult<PageResult<CourseGroupRespVO>> getCourseGroupPage(@Valid CourseGroupPageReqVO pageReqVO) {
        PageResult<CourseGroupDO> pageResult = courseGroupService.getCourseGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程组管理 Excel")
    @PreAuthorize("@ss.hasPermission('train:course-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseGroupExcel(@Valid CourseGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseGroupDO> list = courseGroupService.getCourseGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程组管理.xls", "数据", CourseGroupRespVO.class,
                        BeanUtils.toBean(list, CourseGroupRespVO.class));
    }

}
